package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.constants.Constants;
import com.moli.iov.admin.model.vo.req.CompanyVehicleModelListVo;
import com.moli.iov.admin.model.vo.req.CompanyVehicleModelVo;
import com.moli.iov.admin.service.CompanyVehicleModelService;
import com.moli.iov.base.model.dto.req.CompanyVehicleModelReqDto;
import com.moli.iov.base.model.dto.req.QueryCompanyVehicleModelListParamDto;
import com.moli.iov.base.model.dto.res.CompanyVehicleModelResDto;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ExcelUtil;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 基础设置> 车机型号管理
 *
 * @author whl
 */
@Slf4j
@RequestMapping("/sys")
@RestController
public class CompanyVehicleModelController extends BaseController {

  @Autowired
  private CompanyVehicleModelService companyVehicleModelService;

  /**
   * 车机型号-添加
   */
  @PostMapping("/companyVehicleModel/save")
  public RestResponse saveCompanyVehicleModel(@RequestBody @Validated({
      CompanyVehicleModelVo.Save.class}) CompanyVehicleModelVo modelVo) {
    log.info("添加车机型号 入参：{}", modelVo);
    assembleUserInfo(modelVo);
    CompanyVehicleModelReqDto dto = new CompanyVehicleModelReqDto();
    CommonUtils.copyProperties(modelVo, dto);
    return companyVehicleModelService.saveCompanyVehicleModel(dto);
  }

  /**
   * 车机型号-编辑
   */
  @PostMapping("/companyVehicleModel/update")
  public RestResponse updateCompanyVehicleModel(@RequestBody @Validated({
      CompanyVehicleModelVo.Update.class}) CompanyVehicleModelVo modelVo) {
    log.info("修改车机型号 入参：{}", modelVo);
    assembleUserInfo(modelVo);
    CompanyVehicleModelReqDto dto = new CompanyVehicleModelReqDto();
    CommonUtils.copyProperties(modelVo, dto);
    return companyVehicleModelService.updateCompanyVehicleModel(dto);
  }

  /**
   * 车机-详情
   */
  @PostMapping("/companyVehicleModel/getCompanyVehicleModel")
  public RestResponse<CompanyVehicleModelResDto> getCompanyVehicleModel(
      @RequestBody @Validated({CompanyVehicleModelVo.Read.class}) CompanyVehicleModelVo modelVo) {
    assembleUserInfo(modelVo);
    return companyVehicleModelService.getCompanyVehicleModelDetails(modelVo.getId());
  }

  /**
   * 查询
   */
  @PostMapping("/companyVehicleModel/list")
  public RestResponse listCompanyVehicleModel(
      @RequestBody CompanyVehicleModelListVo vo) {
    assembleUserInfo(vo);
    QueryCompanyVehicleModelListParamDto dto = new QueryCompanyVehicleModelListParamDto();
    CommonUtils.copyProperties(vo, dto);
    return companyVehicleModelService
        .listCompanyVehicleModel(dto);
  }

  /**
   * 导出
   */
  @PostMapping("/companyVehicleModel/export")
  public RestResponse listCompanyVehicleModelExport(
      @RequestBody CompanyVehicleModelListVo vo, HttpServletRequest request,
      HttpServletResponse response) {
    assembleUserInfo(vo);
    log.info("companyVehicleModelListVo");
    QueryCompanyVehicleModelListParamDto dto = new QueryCompanyVehicleModelListParamDto();
    dto.setPageNum((long) 1);
    dto.setPageSize(Constants.EXPORT_PAGE_SIZE);
    CommonUtils.copyProperties(vo, dto);
    RestResponse<PageExtend<CompanyVehicleModelResDto>> res = companyVehicleModelService
        .listCompanyVehicleModel(dto);
    final List data = res.flag &&
        res.getData() != null &&
        res.getData().getRecords() != null ?
        res.getData().getRecords() :
        new ArrayList();
    String[] propNames = {"companyName", "vehicleModel", "deviceModel", "supplierName",
        "vehicleCommand", "gmtCreate"};
    String[] headStr = {"企业名称", "车型", "车机型号", "车机生产商", "支持控车指令", "提交时间"};
    String title = "车机型号";
    try {
      ExcelUtil.exportDataExcel(request, response, data, propNames, headStr, title);
      return RestResponse.success();
    } catch (Exception e) {
      e.printStackTrace();
      return RestResponse.failure("导出异常");
    }

  }
}
